<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('helper.inc');
require_once('crypt.inc');
require_once('settings.inc');

$knopf = 0;
$func = $_REQUEST['func'];

# Auswertungsmenue
if (isset($_REQUEST['ressource'])) { $knopf = 26; $headline = 30; }

$drm = array(true, false, false, false, false, true, false);

require_once('menu.inc');
require_once('knumber.inc');

# Folgende Funktion erlaubt die Auswahl einer Periode fuer die der Statusbericht
# erzeugt werden soll.
#
function Preselect() {
	global $periode;
	global $unum;
	global $menu;
	global $rstufe;

	$db = OpenDB();
?>
<form action="prtressource.php" method="post">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="headline" value="30">
<input type="hidden" name="func" value="prplan">

<table border=0>
<?php
	# Auswahl eines Projekts
	echo "<tr><td>Projektauswahl:</td>\n";
	echo "<td>";

	$query = "select pl_num, pl_prnum, pl_lfd, pr_name from plan, project ";
	$query .= "where pr_num = pl_prnum and pl_status in (2,3,4) ";
	$query .= "order by pl_prnum, pl_lfd desc";

	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	echo "<select name=\"pl_num\">\n";
	$numrows = numrowsDB($result);
	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $pl_num = $data[0];
	   $pl_prnum = $data[1];
	   $pl_lfd = $data[2];
	   $pr_name = $data[3];
	   echo "<option value=\"$pl_num\">$pl_lfd: $pl_prnum $pr_name</option>\n";
	   $rows++;
	}

	echo "</select>";
	echo "</td></tr>\n";
	echo "</table>\n";
	echo "<br><input type=\"submit\" name=\"submit\" value=\"Senden\"> <input type=\"reset\">\n";
	echo "</form>\n";
	closeDB($db);
}

function CreateTempPlan($db, $pl_num, $prnum) {
	global $leneinheit;

	# Als erstes erzeugen wir eine temporaere Tabelle in die wir die
	# einzelnen Daten unseres Plans zusammentragen und korrekt zuordnen.
	#
	$query = "create local temporary table TempPlan (";
	$query .= "tmp_lfd integer not null unique, tmp_miname integer,";
	$query .= "tmp_phase integer, tmp_periode integer, tmp_plan double precision,";
	$query .= "tmp_ist double precision, tmp_taname varchar(50), tmp_notiz varchar(8192),";
	$query .= "constraint \"tempplan_pkey\" primary key (\"tmp_lfd\"))";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	# Nun erzeugen wir einen Plan, ausgehend von den Tasks. Das ist der
	# erste von insgesamt zwei Schritten!
	#
	$query = "select al_hours, al_pstart, al_phase, ta_name, ta_notiz,";
	$query .= "al_ressource from allocation, task where ";
	$query .= "ta_num = al_task and ta_plnum = $pl_num ";
	$query .= "and ta_meeting = 0";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine Tasks!");
	   return false;
	}

	$rows = 0;
	$zaehler = 1;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $al_hours = $data[0];
	   $al_pstart = $data[1];
	   $al_phase = $data[2];
	   $ta_name = $data[3];
	   $ta_notiz = $data[4];
	   $al_ressource = $data[5];

	   $mon = gmdate("n", $al_pstart);
	   $year = gmdate("Y", $al_pstart);
	   $al_pstart = gmmktime(0, 0, 0, $mon, 1, $year);

	   $query = "insert into TempPlan (tmp_lfd, tmp_miname, tmp_phase,";
	   $query .= "tmp_periode, tmp_plan, tmp_ist, tmp_taname, tmp_notiz) ";
	   $query .= "values ($zaehler, $al_ressource, $al_phase, $al_pstart,";
	   $query .= "$al_hours, 0, '$ta_name', '$ta_notiz')";

	   if (!QueryDB($db, $query))
	      return false;

	   $rows++;
	   $zaehler++;
	}

	# Nun suchen wir alle IST-Meldungen und ergaenzen die Daten, sofern
	# sie bereits vorhanden sind, oder fuegen einen neuen Datensatz ein.
	#
	$query = "select wd_minum, wd_datum, wd_hours / $leneinheit, wd_phase, ";
	$query .= "ta_name from wdone, task where ";
	$query .= "wd_prnum = $prnum and ta_num = wd_task";
	$result = QueryDB($db, $query);

	if (!$result)
	   return false;

	$numrows = numrowsDB($result);

	if ($numrows <= 0) {
	   Error("Der von Ihnen gew&auml;hlte Plan $pl_num enth&auml;lt keine IST-Buchungen!");
	   return true;
	}

	$rows = 0;

	while ($rows < $numrows) {
	   $data = fetchDB($result, $rows);
	   $wd_minum = $data[0];
	   $wd_datum = $data[1];
	   $wd_ist = $data[2];
	   $wd_phase = $data[3];
	   $ta_name = $data[4];

	   $mon = gmdate("n", $wd_datum);
	   $year = gmdate("Y", $wd_datum);
	   $per = gmmktime(0, 0, 0, $mon, 1, $year);
	   $query = "select tmp_lfd, tmp_ist from TempPlan where ";
	   $query .= "tmp_miname = $wd_minum and tmp_periode =$per ";
	   $query .= "and tmp_taname = '$ta_name'";
	   $result2 = QueryDB($db, $query);

	   if (!$result2)
	      return false;

	   $nr = numrowsDB($result2);

	   if ($nr > 0) {
	      $data = fetchDB($result2, 0);
	      $tmp_lfd = $data[0];
	      $tmp_ist = $data[1];

	      $wd_ist += $tmp_ist;

	      $query = "update TempPlan set tmp_ist = $wd_ist ";
	      $query .= "where tmp_lfd = $tmp_lfd";

	      if (!QueryDB($db, $query))
	         return false;
	   } else {
	      $mon = gmdate("n", $wd_datum);
	      $year = gmdate("Y", $wd_datum);
	      $wd_datum = gmmktime(0, 0, 0, $mon, 1, $year);

	      $query = "insert into TempPlan (tmp_lfd, tmp_miname, tmp_phase,";
	      $query .= "tmp_periode, tmp_plan, tmp_ist, tmp_taname) ";
	      $query .= "values ($zaehler, $wd_minum, $wd_phase, $wd_datum,";
	      $query .= "0, $wd_ist, '$ta_name')";

	      if (!QueryDB($db, $query))
	         return false;

	      $zaehler++;
	   }

	   $rows++;
	}

	return true;
}

function PrintRessource($pl_num) {
	global $phase;

	$db = OpenDB();
	# Ermitteln der Projektnummer
	$query = "select pl_prnum from plan where pl_num = $pl_num";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$prnum = $data[0];

	# Folgende Funktion erzeugt uns einen Plan mit zugehoerigen IST-Werten,
	# wobei diese bereits innerhalb einer Periode aufsummiert wurden.
	#
	if (CreateTempPlan($db, $pl_num, $prnum) == false) {
	   closeDB($db);
	   return;
	}

	# Wieviele Perioden gibt es insgesamt?
	$query = "select count(distinct tmp_periode) from TempPlan";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$data = fetchDB($result, 0);
	$numper = $data[0];
	$np = $numper * 2;

	# Zeichnen des Tabellenkopfs
?>
<table border=1>
   <tr>
      <td colspan=2>&nbsp;</td>
      <td colspan=<? echo "$np"; ?> class="stbb">Periode</td>
      <td colspan=2 class="stbb">Summe</td>
      <td colspan=2 class="stbb">Total</td>
   </tr>
   <tr>
      <td rowspan=2 valign="top" class="stbb">Ressource</td>
<?php
	if ($phase)
           echo "<td rowspan=2 valign=\"top\" class=\"stbb\">Phase / Task</td>\n";
        else
           echo "<td rowspan=2 valign=\"top\" class=\"stbb\">Task</td>\n";

	$query = "select distinct tmp_periode from TempPlan order by tmp_periode";
	$result = QueryDB($db, $query);

	if (!$result) {
	   echo "</tr></table>";
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$r = 0;

	while ($r < $numrows) {
	   $data = fetchDB($result, $r);
	   $tmp_periode = $data[0];

	   $mon = gmdate("n", $tmp_periode);
	   $year = gmdate("Y", $tmp_periode);

	   if ($r == 0) {
	      $fmon = $mon;
	      $fyear = $year;
	   }

	   echo "      <td colspan=2 class=\"stbb\">$mon/$year</td>\n";
	   $sumperp[$r] = 0.0;
	   $sumperi[$r] = 0.0;
	   $r++;
	}

	$lmon = $mon;
	$lyear = $year;
	echo "      <td colspan=2>&nbsp;</td>\n";
	echo "      <td colspan=2>&nbsp;</td>\n";
	echo "   </tr>\n   <tr>\n";
	$r = 0;

	while ($r < $numrows) {
	   echo "      <td class=\"stbb\">Plan</td>\n";
	   echo "      <td class=\"stbb\">IST</td>\n";
	   $r++;
	}

	echo "      <td class=\"stbb\">Plan</td>\n";
	echo "      <td class=\"stbb\">IST</td>\n";
	echo "      <td class=\"stbb\">Plan</td>\n";
	echo "      <td class=\"stbb\">IST</td>\n   </tr>\n";

	# Nun fuellen wir die Tabelle mit Daten...
	#
	$query = "select tmp_miname, tmp_phase, tmp_periode, tmp_plan, tmp_ist, ";
	$query .= "tmp_taname, mi_nname, mi_vname from TempPlan, mitarbeiter ";
	$query .= "where mi_num = tmp_miname order by mi_nname, mi_vname, ";
	$query .= "tmp_phase, tmp_taname, tmp_periode";
	$result = QueryDB($db, $query);

	if (!$result) {
	   closeDB($db);
	   return;
	}

	$numrows = numrowsDB($result);
	$r = 0;
	$pz = 0;
	$first = true;
	$last = false;
	$sump = 0.0;
	$totp = 0.0;
	$sumptotal = 0.0;
	$sumi = 0.0;
	$toti = 0.0;
	$sumitotal = 0.0;
	$oldname = 0;
	$oldtaname = "";
	$oldphase = -1;
	$class = "nw";
	$ldate = gmmktime(0, 0, 0, $lmon, 1, $lyear);
	$adate = gmmktime(0, 0, 0, $fmon, 1, $fyear);
	$flag = 0;

	while ($r < $numrows) {
	   $data = fetchDB($result, $r);
	   $tmp_miname = $data[0];
	   $tmp_phase = $data[1];
	   $tmp_periode = $data[2];
	   $tmp_plan = $data[3];
	   $tmp_ist = $data[4];
	   $tmp_taname = $data[5];
	   $mi_nname = $data[6];
	   $mi_vname = $data[7];

	   $mon = gmdate("n", $tmp_periode);
	   $year = gmdate("Y", $tmp_periode);

	   if ($r == 0) {
	      $oldname = $tmp_miname;
	      $oldphase = $tmp_phase;
	      $oldtaname = $tmp_taname;
	   }

	   if ($oldphase != $tmp_phase || $oldname != $tmp_miname || $oldtaname != $tmp_taname) {
	      while ($adate <= $ldate) {
	         echo "<td class=\"$class\" align=\"right\">&nbsp;</td><td class=\"$class\" align=\"right\">&nbsp;</td>\n";
	         $aktmon++;

	         if ($aktmon > 12) {
	            $aktyear++;
	            $aktmon = 1;
	         }

	         $adate = gmmktime(0, 0, 0, $aktmon, 1, $aktyear);

	         if ($class == "nw")
	            $class = "nw1";
	         else
	            $class = "nw";
	      }

	      printf ("<td class=\"sum\" align=\"right\">%s</td>", FormatNum($sump, 3));
	      printf ("<td class=\"sum\" align=\"right\">%s</td>\n", FormatNum($sumi, 3));
	      echo "<td colspan=2>&nbsp;</td></tr>\n";

	      if ($oldname != $tmp_miname) {
		 echo "<tr><td colspan=2>&nbsp;</td>";
		 $pz = 0;

		 while ($pz < $numper) {
		    printf ("<td class=\"lsum\" align=\"right\">%s</td>", FormatNum($sumperp[$pz], 3));
		    printf ("<td class=\"lsum\" align=\"right\">%s</td>", FormatNum($sumperi[$pz], 3));
		    $sumperp[$pz] = 0.0;
		    $sumperi[$pz] = 0.0;
		    $pz++;
		 }

	         echo "<td colspan=2>&nbsp;</td>";
	         printf("<td class=\"lsum\" align=\"right\">%s</td>", FormatNum($totp, 3));
	         printf("<td class=\"lsum\" align=\"right\">%s</td></tr>\n", FormatNum($toti, 3));
	         $oldname = $tmp_miname;
	         $totp = 0.0;
	         $toti = 0.0;
	      }

	      $oldphase = $tmp_phase;
	      $oldtaname = $tmp_taname;
	      $sump = 0.0;
	      $sumi = 0.0;
	      $pz = 0;
	      $first = true;
	      $last = true;
	      $class = "nw";
	   }

	   if ($first) {
	      echo "<tr><td class=\"nw\">$mi_nname $mi_vname</td>\n";

	      if ($phase)
	         echo "<td class=\"nw\">$tmp_phase: $tmp_taname</td>\n";
	      else
	         echo "<td class=\"nw\">$tmp_taname</td>\n";

	      $aktmon = $fmon;
	      $aktyear = $fyear;
	      $adate = gmmktime(0, 0, 0, $aktmon, 1, $aktyear);
	      $first = false;
	      $class = "nw";
	      $pz = 0;
	   }

	   if ($mon != $aktmon || $year != $aktyear) {
	      while ($adate < $ldate && $adate != $tmp_periode) {
	         echo "<td class=\"$class\" align=\"right\">&nbsp;</td><td class=\"$class\" align=\"right\">&nbsp;</td>\n";
	         $aktmon++;
	         $pz++;

	         if ($aktmon > 12) {
	            $aktyear++;
	            $aktmon = 1;
	         }

	         $adate = gmmktime(0, 0, 0, $aktmon, 1, $aktyear);

	         if ($class == "nw")
	            $class = "nw1";
	         else
	            $class = "nw";
	      }
	   }

	   printf ("<td class=\"$class\" align=\"right\">%s</td>", FormatNum($tmp_plan, 3));

	   if (($tmp_plan - $tmp_ist) < 0) {
	      $class = "five";

	      if ($class == "nw")
	         $flag = 1;
	   }

	   printf ("<td class=\"$class\" align=\"right\">%s</td>\n", FormatNum($tmp_ist, 3));
	   $aktmon++;

	   if ($aktmon > 12) {
	      $aktmon = 1;
	      $aktyear++;
	   }

	   $adate = gmmktime(0, 0, 0, $aktmon, 1, $aktyear);

	   if ($flag)
	      $class = "nw";

	   if ($class == "nw")
	      $class = "nw1";
	   else
	      $class = "nw";

	   $sump += $tmp_plan;
	   $sumi += $tmp_ist;
	   $totp += $tmp_plan;
	   $toti += $tmp_ist;
	   $sumptotal += $tmp_plan;
	   $sumitotal += $tmp_ist;
	   $sumperp[$pz] += $tmp_plan;
	   $sumperi[$pz] += $tmp_ist;
	   $pz++;
	   $last = false;
	   $r++;
	}

	while ($adate <= $ldate) {
	   echo "<td class=\"$class\" align=\"right\">&nbsp;</td><td class=\"$class\" align=\"right\">&nbsp;</td>\n";
	   $aktmon++;

	   if ($aktmon > 12) {
	      $aktyear++;
	      $aktmon = 1;
	   }

	   $adate = gmmktime(0, 0, 0, $aktmon, 1, $aktyear);

	   if ($class == "nw")
	      $class = "nw1";
	   else
	      $class = "nw";
	}

	printf ("<td class=\"sum\" align=\"right\">%s</td>", FormatNum($sump, 3));
	printf ("<td class=\"sum\" align=\"right\">%s</td>\n", FormatNum($sumi, 3));
	echo "<td colspan=2>&nbsp;</td></tr>\n";

	echo "<tr><td colspan=2>&nbsp;</td>";
	$pz = 0;

	while ($pz < $numper) {
	   printf ("<td class=\"lsum\" align=\"right\">%s</td>", FormatNum($sumperp[$pz], 3));
	   printf ("<td class=\"lsum\" align=\"right\">%s</td>", FormatNum($sumperi[$pz], 3));
	   $pz++;
	}

	printf ("<td colspan=2>&nbsp;</td><td class=\"lsum\" align=\"right\">%s</td>", FormatNum($totp, 3));
	printf ("<td class=\"lsum\" align=\"right\">%s</td></tr>\n", FormatNum($toti, 3));
	$cs = 2 + $np + 2;
	printf ("<tr><td colspan=$cs>&nbsp;</td><td class=\"two\" align=\"right\">%s</td>", FormatNum($sumptotal, 3));
	printf ("<td class=\"two\" align=\"right\">%s</td></tr>\n", FormatNum($sumitotal, 3));
	echo "</table>\n";
	closeDB($db);
}

# Auswertung des Menues:
#
if ($knopf == 26)		// Ressourcenauslastung
   Preselect();

# Auswertung der Funktionen
if ($func == "prplan") {
   $pl_num = $_REQUEST['pl_num'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pl_prnum, pr_name from plan, project ";
   $query .= "where pr_num = pl_prnum and pl_num = $pl_num";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $selpro = $data[0];
   $pr_name = $data[1];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name<br>\n";
   echo "Ressourcenauslastung der Mitarbeiter in PT</p>";
   echo "<br>\n";
   closeDB($db);

   echo "<a href=\"#\" onClick=\"javascript:open_mwindow('prtressource.php?pl_num=$pl_num&header=2&func=PrintPlan', 'Ressourcenauslastung: $selpro $pr_name', 750, 550)\">";
   echo "<img src=\"image/print.png\" border=0 alt=\"Drucken\"></a>\n";

   PrintRessource($pl_num);
}

if ($func == "PrintPlan") {
   $pl_num = $_REQUEST['pl_num'];

   # Ermitteln des Projektnamens und Anzeigen des selben.
   $db = OpenDB();
   $query = "select pl_prnum, pr_name from plan, project ";
   $query .= "where pr_num = pl_prnum and pl_num = $pl_num";
   $result = QueryDB($db, $query);
   $data = fetchDB($result, 0);
   $selpro = $data[0];
   $pr_name = $data[1];
   echo "<p class=\"cry\">Projekt: $selpro $pr_name<br>\n";
   echo "Ressourcenauslastung der Mitarbeiter in PT</p>";
   echo "<br>\n";
   closeDB($db);

   PrintRessource($pl_num);
}

require('footer.inc');
?>
